home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n2.arc / PAINT.C < prev    next >
C/C++ Source or Header  |  1990-06-14  |  3KB  |  101 lines

  1. /* paint.c -- A simple paint program that demonstrates the
  2.  * interactive drawing tools in draw.c. It will automatically
  3.  * use the highest resolution mode available on your system.
  4.  * You can, however, configure it to a specific mode by
  5.  * setting the gmode and gdriver variables to appropriate
  6.  * BGI settings. Note: A mouse is required.
  7.  */
  8. #include <stdio.h>
  9. #include <graphics.h>
  10. #include "draw.h"
  11. #include "mouse.h"
  12.  
  13. void setupgraphics(void);
  14. void drawscreen(void);
  15. void paint(void);
  16.  
  17. int main(void) {
  18.   setupgraphics();
  19.   drawscreen();
  20.   if (!initmouse()) {
  21.     closegraph();
  22.     printf("Mouse required.\n");
  23.     exit(1);
  24.   }
  25.   paint();
  26.   closegraph();
  27.   return(0);
  28. }
  29.  
  30. /* Sets the graphics mode, drawing parameters, and drawing
  31.  * window dimensions.
  32.  */
  33. void setupgraphics(void)
  34. {
  35.   int gmode, gdriver=DETECT, errorcode;
  36.  
  37.   initgraph(&gdriver, &gmode, "\\tc\\bgi");
  38.   if ((errorcode=graphresult()) != grOk) {
  39.     printf("Graphics error: %s\n", grapherrormsg(errorcode));
  40.     exit(1);
  41.   }
  42.   /* Set the coordinates of the drawing window */
  43. /*  wb = 200;
  44.   wl = 100;
  45.   wr = 500;
  46.   */
  47.  
  48.   wt = 1;  wb = 400;
  49.   if (wb > getmaxy()-1) wb = getmaxy() - 1;
  50.   wl = textwidth("MenuWidth");
  51.   wr = getmaxx() - 1;
  52.  
  53.   globaldrawcolor = WHITE;
  54. }
  55.  
  56. /* Draws the paint program screen. First it sets the screen
  57.  * to blue, then blanks out the drawing window, and finally
  58.  * writes out a menu to the left of the screen.
  59.  */
  60. void drawscreen(void)
  61. {
  62.   setfillstyle(SOLID_FILL, EGA_BLUE);
  63.   bar(0, 0, getmaxx(), getmaxy());
  64.   setfillstyle(SOLID_FILL, EGA_BLACK);
  65.   bar3d(wl-1, wt-1, wr+1, wb+1, 1, 0);
  66.   rectangle(0, 0, wl-1, 120);
  67.   outtextxy(4, 10, "Pencil");   line(0, 20, wl-1, 20);
  68.   outtextxy(4, 30, "SprayCan"); line(0, 40, wl-1, 40);
  69.   outtextxy(4, 50, "Erase");    line(0, 60, wl-1, 60);
  70.   outtextxy(4, 70, "Lines");    line(0, 80, wl-1, 80);
  71.   outtextxy(4, 90, "Circle");   line(0, 100, wl-1, 100);
  72.   outtextxy(4, 110, "Quit");
  73. }
  74.  
  75. /* The interactive routine that is used to select the
  76.  * various paint routines.
  77.  */
  78. void paint(void)
  79. {
  80.   int x, y;
  81.  
  82.   while (!buttonstatus(PRESSED,LEFT_BUTTON)) ;
  83.   while (1) {
  84.     while (!buttonstatus(RELEASED,LEFT_BUTTON)) ;
  85.     getmousecoords(&x, &y);
  86.     /* Is the mouse over a menu option? If so, execute
  87.      * its corresponding drawing function.
  88.      */
  89.     if (x < wl && y < 120) {
  90.       if (y < 20) pencil();
  91.       else if (y < 40) spraycan();
  92.       else if (y < 60) erase();
  93.       else if (y < 80) drawlines();
  94.       else if (y < 100) drawcircle();
  95.       else return;  /* Quit option */
  96.     }
  97.     else  /* Flush the extra button press */
  98.       while (!buttonstatus(PRESSED,LEFT_BUTTON)) ;
  99.   }
  100. }
  101.